package controllers.cms.blocks;

import java.util.List;

import models.BeanDtoInfo;
import models.cms.BlockDto;
import models.cms.PageDto;
import play.api.templates.Html;
import services.BaseService;
import services.tools.ServiceUrlFormatter;
import controllers.cms.CmsException;
import controllers.cms.TemplateResolver;

public class BeansManagerImpl implements BlockManager {

	@Override
	public Html render(final PageDto p, final BlockDto b) throws CmsException {
		List<BeanDtoInfo> results = (List<BeanDtoInfo>) BaseService.getCollection(BeanDtoInfo.class, ServiceUrlFormatter.format(b.getUrl(), p.getParams()),null).get();
		if(results != null && results.size() > b.getLimit()){
			results = results.subList(0, b.getLimit() -1);
		}
		b.addData("contents",results);
		return TemplateResolver.render(p, b);
	}
}
